package ui 
{
	import com.GCCity;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import data.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import org.flixel.FlxG;
	
	import ui.textfield.GCStylizedText;
	import ui.textfield.GCTextField;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class CitySprite extends Sprite
	{
		private static const ICON_DIMENSION:uint = 30;
		
		private var _cityId:int;
		private var _selectableArea:Sprite;
		private var _nameDisplay:GCTextField;
		private var _restrictedTravelDisplay:GCTextField;
		public var _toggle:Boolean;
		
		private var _cityInformation:Sprite;
		private var _harvestableResources:Sprite;
		private var _workersArea:Sprite;
		private var _workersLabel:GCTextField;
		private var _buildingsArea:Sprite;
		private var _buildingsLabel:GCTextField;
		
		private var _blinkTimer:Timer;
		private var _blinkDelta:Number;
		
		/**
		 * Constructor
		 * @param	name
		 */
		public function CitySprite(name:int) 
		{
			_cityId = name;
			_selectableArea = new Sprite;
			_nameDisplay = new GCTextField();
			_restrictedTravelDisplay = new GCTextField();
			_toggle = true;
			
			_cityInformation = new Sprite();
			_harvestableResources = new Sprite();
			_buildingsArea = new Sprite();
			_buildingsLabel = new GCTextField();
			_workersArea = new Sprite();
			_workersLabel = new GCTextField();
			
			_blinkTimer = new Timer(100);
			_blinkDelta = 0.1;
			
			init();			
		}
		
		/**
		 * Function to disable the city 
		 */
		public function disableCity():void
		{
			this.alpha = 0.5;
			_toggle = false;
		}
		
		public function enableCity():void
		{
			this.alpha = 1;
			_toggle = true;
		}
		
		public function highlightCity(doHighlight:Boolean):void
		{
			if (doHighlight)
			{
				_selectableArea.graphics.lineStyle(1);
				_selectableArea.graphics.beginFill(GCConstants.COLOR_RED, 0.7);
				_selectableArea.graphics.drawCircle(_selectableArea.width / 2, _selectableArea.width / 2, _selectableArea.width);
				_selectableArea.graphics.endFill();
				
				_blinkTimer.start();
			}
			else
			{
				_selectableArea.graphics.clear();
			}
		}
		
		private function init():void
		{	
			_blinkTimer.addEventListener(TimerEvent.TIMER, onBlink);
			this.addEventListener(MouseEvent.ROLL_OVER, onMouseEnter);
			this.addEventListener(MouseEvent.ROLL_OUT, onMouseExit);
			
			_selectableArea.addChild(GCResources.getResource("City"));
			this.addChild(_selectableArea);
			
			var coord:Point = GCCityData.getCoordinatesFromId(_cityId);
			setXY(coord.x, coord.y);

			_nameDisplay.setXY(0, 0);
			_nameDisplay.autoSize = TextFieldAutoSize.LEFT;
			_nameDisplay.setStyle(GCTextField.TEXT_TYPE_CITY_NAME);
			_nameDisplay.text = GCCityData.getNameFromId(_cityId);
			_cityInformation.addChild(_nameDisplay);
			
			_restrictedTravelDisplay.setXY(0, -10);//_nameDisplay.x, _nameDisplay.y + _nameDisplay.height);
			_restrictedTravelDisplay.autoSize = TextFieldAutoSize.LEFT;
			_restrictedTravelDisplay.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
			_restrictedTravelDisplay.text = "You cannot travel here now."
			_restrictedTravelDisplay.visible = false;
			
			var infoCoordinate:Point = GCCityData.getInfoCoordinateFromId(_cityId);
			_cityInformation.x = infoCoordinate.x;
			_cityInformation.y = infoCoordinate.y;
			
			_cityInformation.visible = false;
			this.addChild(_cityInformation);
			this.addChild(_restrictedTravelDisplay);
			
			
			// Displaying harvestable resources at each city
			var resources:Vector.<uint> = GCCityData.getResourcesFromId(_cityId);
			var resourceLabel:GCTextField = new GCTextField();
			resourceLabel.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
			resourceLabel.autoSize = TextFieldAutoSize.LEFT;
			resourceLabel.text = "Resources Here:" + "\n";
			_harvestableResources.y += _nameDisplay.height;
			_harvestableResources.addChild(resourceLabel);
			var yOffset:uint = resourceLabel.height;
			for each(var resourceId:uint in resources)
			{
				var resourceImageArea:Sprite = new Sprite();
				var resourceName:String = GCItemData.getNameFromId(resourceId);
				var resourceIcon:Bitmap = GCResources.getResource(resourceName);
				resourceIcon.width = ICON_DIMENSION;
				resourceIcon.height = ICON_DIMENSION;
				resourceImageArea.addChild(resourceIcon);
				
				var resourceText:GCTextField = new GCTextField();
				resourceText.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
				resourceText.autoSize = TextFieldAutoSize.LEFT;
				resourceText.setXY(resourceIcon.x + resourceIcon.width, resourceIcon.y);
				resourceText.text = resourceName.concat("\n");
				resourceImageArea.addChild(resourceText);
				
				resourceImageArea.y = yOffset;
				yOffset += resourceImageArea.height;
				_harvestableResources.addChild(resourceImageArea);
			}
			
			_cityInformation.addChild(_harvestableResources);
			
			_buildingsLabel.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
			_buildingsLabel.autoSize = TextFieldAutoSize.LEFT;
			_buildingsLabel.text = "Your Buildings Here:";
			_buildingsLabel.width = 150;
			_buildingsArea.x = _harvestableResources.x;
			_buildingsArea.y = _harvestableResources.y + _harvestableResources.height;
			_buildingsArea.addChild(_buildingsLabel);
			_cityInformation.addChild(_buildingsArea);
			
			_workersArea.x = _buildingsArea.x;
			_workersArea.y = _buildingsArea.y + _buildingsArea.height;
			_workersLabel.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
			_workersLabel.autoSize = TextFieldAutoSize.LEFT;
			_workersLabel.text = "Workers Here: " + String(0);
			_workersArea.addChild(_workersLabel);
			_cityInformation.addChild(_workersArea);
			
			redrawInfo();
		}
		
		/**
		 * Redraw parts of the city information that can vary over time, i.e. the
		 * buildings and workers
		 */
		public function redrawInfo():void
		{
			while (_buildingsArea.numChildren > 1)
			{
				_buildingsArea.removeChildAt(1);
			}
			
			var city:GCCity = GCPlayState.getCityData(_cityId);
			
			var constructedBuildings:Vector.<uint> = city.getAllAlreadyConstructed();
			var bldgYOffset:uint = _buildingsLabel.height;
			for each(var bldgId:uint in constructedBuildings)
			{
				var bldgImageArea:Sprite = new Sprite();
				var bldgName:String = GCBuildingData.getNameFromId(bldgId);
				var bldgIcon:Bitmap = GCResources.getResource(bldgName);
				bldgIcon.width = ICON_DIMENSION;
				bldgIcon.height = ICON_DIMENSION;
				bldgImageArea.addChild(bldgIcon);
				
				var bldgText:GCTextField = new GCTextField();
				bldgText.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
				bldgText.autoSize = TextFieldAutoSize.LEFT;
				bldgText.setXY(bldgIcon.x + bldgIcon.width, bldgIcon.y);
				bldgText.text = bldgName.concat("\n");
				bldgImageArea.addChild(bldgText);
				
				bldgImageArea.y = bldgYOffset;
				bldgYOffset += bldgImageArea.height;
				_buildingsArea.addChild(bldgImageArea);
			}
			
			if (constructedBuildings.length == 0)
			{
				var noneText:GCTextField = new GCTextField();
				noneText.setStyle(GCTextField.TEXT_TYPE_CITY_MAP);
				noneText.autoSize = TextFieldAutoSize.LEFT;
				noneText.setXY(0, bldgYOffset);
				noneText.text = "None";
				_buildingsArea.addChild(noneText);
			}
			
			_workersArea.x = _buildingsArea.x;
			_workersArea.y = _buildingsArea.y + _buildingsArea.height;
			var numWorkers:uint = city.totalWorkers;
			_workersLabel.text = "Total Workers here: " + String(numWorkers);
			
			_cityInformation.graphics.clear();
			_cityInformation.graphics.lineStyle(1);
			_cityInformation.graphics.beginFill(GCConstants.COLOR_WHITE);
			_cityInformation.graphics.drawRoundRect(-5, -5, _cityInformation.width + 5, _cityInformation.height + 5, 10);
			_cityInformation.graphics.endFill();
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------		
		
		private function onMouseEnter(event:MouseEvent):void
		{
			_cityInformation.visible = true;
			_blinkTimer.stop();
			_selectableArea.alpha = 1.0;
			
			if (!_toggle)
				_restrictedTravelDisplay.visible = true;
		}
		
		private function onMouseExit(event:MouseEvent):void
		{
			_cityInformation.visible = false;
			_restrictedTravelDisplay.visible = false;
		}
		
		private function onBlink(event:TimerEvent):void
		{
			if (_selectableArea.alpha >= 1.0 && _blinkDelta > 0)
				{
					_blinkDelta *= -1.0;
				}
				else if (_selectableArea.alpha < 0.5 && _blinkDelta < 0)
				{
					_blinkDelta *= -1.0;
				}
				_selectableArea.alpha += _blinkDelta;
		}
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------		
		
		// Returns the name of the city
		public function getCityId():int
		{
			return _cityId;
		}
				
		//----------------------------------------------------------------------
		// SET FUNCTIONS
		//----------------------------------------------------------------------		

		// Sets the coordinate of the city
		public function setXY(x:int, y:int):void
		{
			this.x = x;
			this.y = y;
		}
	}
}